package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.CoopDao;
import com.hhit.collaborative_learning.dao.impl.CoopDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

/**
 * Servlet implementation class SetCoopProjServlet
 */
@WebServlet("/communication/SetCoopProjServlet")
public class SetCoopProjServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SetCoopProjServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tyle = request.getParameter("tyle");
		String projID = request.getParameter("projID");
		Connection con = DBAccess.getInstance().getConn();
		JSONObject jobj = new JSONObject();
		switch (tyle) {
		case "0":
			System.out.println("类型  " + tyle + " 修改");
			String title = request.getParameter("title");
			String desc = request.getParameter("desc");
			jobj = updateCoopProj(con, projID, title, desc);
			break;
		case "1":
			System.out.println("类型  " + tyle + " 删除");
			jobj=deleteCoopproj(con,projID);
			break;

		default:
			System.out.println("非法 类型  " + tyle + " ");
			break;
		}
		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
			System.out.println("update coop json "+jobj.toString());
		}
	}

	private JSONObject deleteCoopproj(Connection con, String projID) {
		// TODO Auto-generated method stub
		CoopDao cd = new CoopDaoImpl();
		JSONObject jobj = new JSONObject();
		String isSuccess = "false";
		if (!StringUtils.isNullOrEmpty(projID)) {
			projID = " ID=" + projID+" ";
		}
		int re=cd.delete(con, projID);
		if (re != 0) {
			isSuccess = "true";
		}
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

	private JSONObject updateCoopProj(Connection con, String projID,
			String title, String desc) {
		// TODO Auto-generated method stub
		CoopDao cd = new CoopDaoImpl();
		JSONObject jobj = new JSONObject();
		String isSuccess = "false";
		if (!StringUtils.isNullOrEmpty(projID)) {
			projID = " ID=" + projID+" ";
		}
		int re = cd.update(con, projID, title, desc);
		if (re != 0) {
			isSuccess = "true";
		}
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

}
